package com.sphd.emanu.fragment.dispose;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.msg.MsgActivity;
import com.sphd.emanu.adapter.MsgAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.login.UserMessage;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * “处理” fragment
 *
 * @Author Hu
 * @Time 2017/4/5 -- 17:23.
 * @Email 87683202@qq.com
 */

public class DisposeFragment extends MVPBaseFragment<DisposeContract.View, DisposePresenter> implements DisposeContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.untreated)
    TextView untreated;
    @BindView(R.id.processed)
    TextView processed;

    Unbinder unbinder;
    private static final String TAG = "DisposeFragment";
    Bundle bundle;
    User user;
    Integer state = 1;
    Integer curpage = 1;//当前第几页
    Integer pageno = 10;//一页多少条
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    MsgAdapter msgAdapter;
    Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dispose;
    }

    @Override
    protected void initView() {
        intent = new Intent();
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(this);
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                UserMessage userMessage = (UserMessage) adapter.getItem(position);
                bundle.putSerializable(Constant.USER_MSG, userMessage);
                intent.setClass(context, MsgActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, Constant.REQUEST_CODE);
            }
        });
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecycler);
        msgAdapter = new MsgAdapter(R.layout.item_msg, new ArrayList<UserMessage>());
        msgAdapter.setEmptyView(emptyView);
        msgAdapter.openLoadAnimation();
        msgAdapter.setOnLoadMoreListener(this, myRecycler);
        myRecycler.setAdapter(msgAdapter);
    }

    @Override
    protected void initData() {
        showProgressDialog();
        mPresenter.listMessage(state, user.getUserid(), curpage, pageno);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.untreated, R.id.processed})
    public void onViewClicked(View view) {
        initColor();
        curpage = 1;
        switch (view.getId()) {
            case R.id.untreated:
                setTextColor(untreated, R.color.colorPrimary);
                state = 1;
                break;
            case R.id.processed:
                setTextColor(processed, R.color.colorPrimary);
                state = 2;
                break;
        }
        initData();
    }

    private void initColor() {
        setTextColor(untreated, R.color.my_gray);
        setTextColor(processed, R.color.my_gray);
    }

    private void setTextColor(TextView textView, int colorId) {
        textView.setTextColor(ContextCompat.getColor(getContext(), colorId));
    }


    @Override
    public void setListBean(List<UserMessage> userMessages) {
        dissProgressDialog();

        if (curpage == 1) {
            msgAdapter.setNewData(userMessages);

        } else {
            msgAdapter.addData(userMessages);

        }
        if (userMessages.size() != pageno) {
            msgAdapter.loadMoreEnd(false);
        } else {
            msgAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        curpage++;
        initData();
    }

    @Override
    public void onRefresh() {
        curpage = 1;
        initData();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_CODE) {
            onViewClicked(untreated);
        }
    }
}
